# input pins for my esp32 test breadboard

from machine import Pin, ADC  #引入ADC模块

pot = ADC(Pin(1))         #定义34脚为ADC脚，可以读取模拟电压
pot.width(ADC.WIDTH_12BIT) #读取的电压转为0-4096
pot.atten(ADC.ATTN_11DB)   #输入电压0-3.3v

button_val_map = [
    'left', 741,
    'right', 240,
    'center', 600
]
class LiteButton():
    base_val = 0

    def __init__(self, base_val):
        self.base_val = base_val
        
    def value(self):
        pot_value = pot.read() //4  #使读取的电压变为0-1024
        if pot_value>=self.base_val-50 and pot_value<=self.base_val+50:
            return 0
        else:
            return 1
        

class Buttons():
    def __init__(self):
        self.name = "esp32_s3_box_lite"
        self.left = LiteButton(button_val_map[1])
        self.right = LiteButton(button_val_map[3])
        self.center = LiteButton(button_val_map[5])

        self.hyper = 0
        self.thrust = 0
        self.fire = 0
        self.boot = Pin(0, Pin.IN, Pin.PULL_UP)
        
    def button(self):
        if not self.boot.value():
            return 'boot'

        pot_value = pot.read() //4  #使读取的电压变为0-1024
        if pot_value>=self.left.base_val-25 and pot_value<=self.left.base_val+25:
            return 'left'
        elif pot_value>=self.right.base_val-25 and pot_value<=self.right.base_val+25:
            return 'right'
        elif pot_value>=self.center.base_val-25 and pot_value<=self.center.base_val+25:
            return 'center'
        else:
            return None